package com.hui.service.impl;

import com.hui.bean.ClassBean;
import com.hui.bean.StudentBean;
import com.hui.dao.impl.ClassDaoImpl;
import com.hui.dao.impl.StudentDaoImpl;
import com.hui.service.IClassService;

import java.sql.SQLException;
import java.util.List;

/**
 * 班级业务层实现类
 *
 */
public class ClassServiceImpl implements IClassService {

    //卧槽，还可以这种操作？
    private ClassDaoImpl classDao;

    private StudentDaoImpl studentDao;

    public ClassServiceImpl(){
        try {
            classDao = ClassDaoImpl.class.newInstance();
            studentDao=StudentDaoImpl.class.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<ClassBean> getAllClasses() {
//        try {
//            return new ClassDaoImpl().getAllClasses();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        try {
            return classDao.getAllClasses();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public ClassBean getClassByClassName(String className) {
        try {
            return classDao.getClassByName(className);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }


    @Override
    public boolean addClass(ClassBean classBean) {
        int result = 0;
        try {
            result = classDao.saveClass(classBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result == 1;

    }

    @Override
    public boolean removeClass(int classId) {
        List<StudentBean> students = null;
        try {
            students = studentDao.getStudentByClassId(classId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (students.isEmpty()) {
            try {
               return classDao.deleteClassById(classId)==1;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }

        }
        return false;
    }

    @Override
    public boolean isExitByClassName(String className) {
        try {
            ClassBean classBean = classDao.getClassByName(className);
            if (null==classBean.getClassName()) {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public List<ClassBean> getClassListWithLimit(String currentPage) {
        return null;
    }
}
